/* eslint-disable no-restricted-syntax */
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-param-reassign */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */

// @ts-expect-error
export function deepcoyp<T extends Record<string, any>>(origin: T, target?: T = {}) {
  if (origin instanceof Array) {
    // @ts-expect-error
    target = [];
    origin.forEach((item) => deepcoyp(item, target));
  } else {
    for (const k in origin) {
      if (origin.hasOwnProperty(k)) {
        if (typeof origin[k] === 'object' && origin[k]) {
          // @ts-expect-error
          target[k] = origin[k] instanceof Array ? [] : {};
          deepcoyp(origin[k], target[k]);
        } else {
          target[k] = origin[k];
        }
      }
    }
  }

  return target;
}
